package cn.ubeta.b2c.utils.preference;

import android.content.Context;
import android.text.TextUtils;

import cn.ubeta.b2c.GuideActivity;
import cn.ubeta.b2c.bean.AddressInfo;
import cn.ubeta.b2c.bean.CartInfo;
import cn.ubeta.b2c.bean.MeInfo;
import cn.ubeta.b2c.bean.OrderInfo;
import cn.ubeta.b2c.bean.UserInfo;
import cn.ubeta.b2c.utils.JsonUtils;
import cn.ubeta.b2c.utils.ToastHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liudengyong on 16/8/22.
 */

public class UserSetting extends AbstractSetting {

    public UserSetting(Context context, String settingFileName) {
        super(context, settingFileName);
    }

    public void putCartInfo(CartInfo info) {
        putString("cartInfo", info.toString());
    }

    public CartInfo getCartInfo() {
        String value = getString("cartInfo", null);
        return new CartInfo(value);
    }

    public String getUid() {
        return getString("uid", null);
    }

    public void setUid(String uid) {
        putString("uid", uid);
    }

    public String getNickName() {
        return getString("nickName", null);
    }

    public void setNickName(String name) {
        putString("nickName", name);
    }

    public void saveUserInfo(UserInfo info) {
        setUid(info.getUid());
        setNickName(info.getNickName());
    }

    private static final String GUIDE_ID = "GUIDE_ID";

    public void setRunGuide() {
        putString(GUIDE_ID, GuideActivity.IDENTIFY);
    }

    public boolean needRunGuide() {
        return !GuideActivity.IDENTIFY.equals(getString(GUIDE_ID, ""));
    }

    public void savePreConfigAddress(AddressInfo info) {
        putLong("addressId", info.getAddressId());

        putString("addressName", info.getName());
        putString("addressMobile", info.getMobile());
        putString("addressPhone", info.getPhone());

        putInt("addressProvinceId", info.getProvinceId());
        putString("addressProvinceName", info.getProvinceName());
        putInt("addressCityId", info.getCityId());
        putString("addressCityName", info.getCityName());
        putInt("addressCountyId", info.getCountyId());
        putString("addressCountyName", info.getCountyName());
        putInt("addressTownId", info.getTownId());
        putString("addressTownName", info.getTownName());

        putString("addressDetail", info.getAddressDetail());
        putString("addressAddressName", info.getAddressName());
    }

    public AddressInfo getPreConfigAddressInfo() {
        AddressInfo info = new AddressInfo();
        info.setAddressId(getLong("addressId", 0L));

        info.setName(getString("addressName", null));
        info.setMobile(getString("addressMobile", null));
        info.setPhone(getString("addressPhone", null));

        info.setProvinceId(getInt("addressProvinceId", 0));
        info.setProvinceName(getString("addressProvinceName", null));
        info.setCityId(getInt("addressCityId", 0));
        info.setCityName(getString("addressCityName", null));
        info.setCountyId(getInt("addressCountyId", 0));
        info.setCountyName(getString("addressCountyName", null));
        info.setTownId(getInt("addressTownId", 0));
        info.setTownName(getString("addressTownName", null));

        info.setAddressDetail(getString("addressDetail", null));
        info.setAddressName(getString("addressAddressName", null));
        return info;
    }

    public String getPreConfigJdPayCardTitle() {
        String json =  getString("jdPayCardInfo", null);
        if (!TextUtils.isEmpty(json)) {
            return JsonUtils.getString(json, "title");
        } else {
            return null;
        }
    }

    public void savePreConfigJdPayCardInfo(String json) {
        putString("jdPayCardInfo", json);
    }

    public void savePreConfigRecvTime(int recvTime) {
        putInt("recvTime", recvTime);
    }

    public int getPreConfigRecvTime() {
        return getInt("recvTime", 2);
    }

    public void savePreConfigBillInfo(String title, boolean isElec, String mobile) {
        putString("billTitle", title);
        putBoolean("billIsElec", isElec);
        putString("billMobile", mobile);
    }

    public String getPreConfigBillInfo() {
        StringBuilder sb = new StringBuilder("普通 ");
        String title = getString("billTitle", null);
        if (TextUtils.isEmpty(title)) {
            sb.append("个人");
        } else {
            sb.append(title);
        }
        if (getBoolean("billIsElec", true)) {
            sb.append(" 电子");
        } else {
            sb.append(" 纸质");
        }
        sb.append("发票");
        return sb.toString();
    }

    public String getPreConfigBillTitle() {
        return getString("billTitle", null);
    }

    public String getPreConfigBillMobile() {
        return getString("billMobile", null);
    }

    public boolean getPreConfigBillIsElec() {
        return getBoolean("billIsElec", true);
    }

    public List<OrderInfo> getOrders() {
        String orders = getString("orders", "");
        String[] token = orders.split(";");
        List<OrderInfo> data = new ArrayList<>();
        if (token != null) {
            for (String str : token) {
                try {
                    data.add(OrderInfo.parse(str));
                } catch (Exception e) {
                    ToastHelper.debug("解析订单错误：" + e.getMessage());
                }
            }
        }
        return data;
    }

    public void saveOrder(OrderInfo info) {
        String orders = getString("orders", "");
        String[] token = orders.split(";");
        StringBuilder sb = new StringBuilder();
        if (token != null) {
            int i = 0;
            for (String str : token) {
                if (i++ > 50) {
                    break;
                }
                sb.append(";" + str);
            }
        }
        putString("orders", info.toString() + sb.toString());
    }

    public void saveMeInfo(MeInfo info) {
        putString("nickName", info.getNickname());
        putString("avatar", info.getAvatar());
    }

    public String getAvatar() {
        return getString("avatar", null);
    }

    public void setOrderPaid(long orderId) {
        List<OrderInfo> data = getOrders();
        for (OrderInfo info : data) {
            if (info.getOrderId() == orderId) {
                info.setTime(OrderInfo.PAID_FLAG);
                break;
            }
        }
        StringBuilder sb = new StringBuilder();
        for (OrderInfo info : data) {
            if (sb.length() == 0) {
                sb.append(info.toString());
            } else {
                sb.append(";" + info.toString());
            }
        }
        putString("orders", sb.toString());
    }
}
